package com.persondemo.videoappliction.ui.duanzi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.DuanziBean;
import com.persondemo.videoappliction.bean.ZanCaiBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.event.MainEvent;
import com.persondemo.videoappliction.ui.adapter.DuanzIDitlsAdapter;
import com.persondemo.videoappliction.ui.base.BaseFragment;
import com.persondemo.videoappliction.ui.duanzi.contract.DuanziContract;
import com.persondemo.videoappliction.ui.duanzi.presenter.DuanziPresenter;
import com.persondemo.videoappliction.ui.image.ImageDetailsActivity;
import com.persondemo.videoappliction.utils.FalseClickListener;
import com.persondemo.videoappliction.utils.FalseClickListener02;
import com.persondemo.videoappliction.utils.PreferenceUtil;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.CustomLoadMoreView;
import com.persondemo.videoappliction.widget.SpaceItemDecoration;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2018/4/19.
 */

public class MeiZiFramgent extends BaseFragment<DuanziPresenter> implements DuanziContract.View {
    private static final String TYPE = "2";
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.SimpleMultiStateView)
    com.persondemo.videoappliction.widget.SimpleMultiStateView SimpleMultiStateView;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    Unbinder unbinder;
    private int page = 1;
    private DuanzIDitlsAdapter adapter;
    private List<DuanziBean.DataBean> list;
    private static final String TYPE_01 = "1";//点赞
    private static final String TYPE_02 = "2";//踩
    private static final String TYPE_03 = "3";//收藏
    private static final String TYPE_04 = "4";//对评论点赞
    private String imei = "";
    private int postion = 0;

    public static MeiZiFramgent newInstance() {
        Bundle args = new Bundle();
        MeiZiFramgent fragment = new MeiZiFramgent();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_fragment_duanzi001;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (mPresenter != null) {
                    page = 1;
                    showLoadingDialog();
                    mPresenter.getdata(TYPE, page + "","");
                }
            }
        });
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        adapter = new DuanzIDitlsAdapter(list, getActivity());
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(20));
        mRecyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                godeitls(list.get(position).getId() + "");
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getdata(TYPE, page + "",list.get(list.size()-1).getId()+"");
            }
        }, mRecyclerView);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                postion = position;
                imei = PreferenceUtil.getString("imei", "");
                Log.i("lidaming", "imei" + imei);
                if (TextUtils.isEmpty(imei)) {
                    T.showLong(getActivity(), "你还未登录，请同意获取手机权限imei,如果拒绝，请前往手机设置获取权限！");
                    return;
                }
                switch (view.getId()) {
                    case R.id.item_duanzi_liner:
                        Intent i = new Intent(getActivity(), ImageDetailsActivity.class);
                        i.putStringArrayListExtra("list_img", (ArrayList<String>) list.get(position).getImgs());
                        getActivity().startActivity(i);
                        break;
                    case R.id.dianzan_liner:
                        if (FalseClickListener.isFastDoubleClick()) {
                            T.showShort(getActivity(), "你点击过于频繁，请勿重复点击！");
                            return;
                        }
                        if (mPresenter != null) {
                            mPresenter.zancaiping(list.get(position).getId() + "", TYPE_01, imei);
                        }
                        break;
                    case R.id.cai_liner:
                        if (FalseClickListener02.isFastDoubleClick()) {
                            T.showShort(getActivity(), "你点击过于频繁，请勿重复点击！");
                            return;
                        }
                        if (mPresenter != null) {
                            mPresenter.zancaiping(list.get(position).getId() + "", TYPE_02, imei);
                        }
                        break;
                    case R.id.collect_liner:
                        if (mPresenter != null) {
                            mPresenter.zancaiping(list.get(position).getId() + "", TYPE_03, imei);
                        }
                        break;
                    case R.id.comment_liner:
                        godeitls(list.get(position).getId() + "");
                        break;
                }
            }
        });
    }

    private void godeitls(String ids) {
        Intent intent = new Intent(getActivity(), DuanziDetilsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", ids);
        bundle.putInt("type",1);
        intent.putExtras(bundle);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        if (mPresenter != null) {
            showLoadingDialog();
            mPresenter.getdata(TYPE, page + "","");
        }
    }

    @Override
    public void showFaild() {
        super.showFaild();
        hideLoadingDialog();
        T.showShort(getActivity(), "加载失败！");
    }

    @Override
    public void loaddata(DuanziBean duanziBean) {
        hideLoadingDialog();
        if (duanziBean == null || duanziBean.getStatus() != 1) {
            showFaild();
            return;
        }
        if (duanziBean.getData() == null || duanziBean.getData().size() == 0) {
            hideLoadingDialog();
            T.showLong(getContext(), "没有更多数据了！");
            return;
        }
        if (duanziBean.getStatus() == 1 && duanziBean.getData().size() > 0) {
            list.clear();
            list = duanziBean.getData();
            adapter.setNewData(list);
            mPtrFrameLayout.refreshComplete();
            showSuccess();
            page++;
            hideLoadingDialog();

        }
    }

    @Override
    public void loadmore(DuanziBean duanziBean) {
        if (duanziBean == null || duanziBean.getData().size() == 0) {
            adapter.loadMoreEnd();
            T.showLong(getActivity(), "没有更多数据了！");
            return;
        } else {
            page++;
            adapter.addData(duanziBean.getData());
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void getzcp(ZanCaiBean zanCaiBean) {
        if (zanCaiBean.getStatus() == 1) {
            if (zanCaiBean.getData().getType().equals("1")) {
                DuanziBean.DataBean dataBean = (DuanziBean.DataBean) adapter.getItem(postion);
                list.get(postion).setSupport(zanCaiBean.getData().getNum());
                adapter.notifyItemChanged(postion);
            } else if (zanCaiBean.getData().getType().equals("2")) {
                DuanziBean.DataBean dataBean = (DuanziBean.DataBean) adapter.getItem(postion);
                list.get(postion).setOpposition(zanCaiBean.getData().getNum());
                adapter.notifyItemChanged(postion);
            } else if (zanCaiBean.getData().getType().equals("3")) {
                DuanziBean.DataBean dataBean = (DuanziBean.DataBean) adapter.getItem(postion);
                list.get(postion).setShou(zanCaiBean.getData().getNum());
                adapter.notifyItemChanged(postion);
            }
        } else {
            T.showShort(getActivity(), zanCaiBean.getMsg());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Subscriber
    private void gomovie(MainEvent event) {
        if (event == null) return;
        String title = event.title;
        if (title.equals("更新用户信息")){
            if (mPresenter != null) {
                showLoadingDialog();
                Log.i("lidaming","成功了吗");
                mPresenter.getdata(TYPE, page + "","");
            }
        }
    }
}
